/**
 * 通用型组件的组装 增删改查
 */
export const Slime = {
  data() {
    return {
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParam: {},
      /* table加载状态 */
      loading: false,
      /* 弹窗加载状态 */
      confirmLoading: false,
      /* 表单显示 */
      visible: false,
      /* 表单属性 */
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条';
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        size:'default',
      },
      /* table选中keys*/
      selectedRowKeys: []
    };
  },
  created() {},
  methods: {
    //封装初始话list
    loadDataPack(arg){ // arg=1 会进行页码初始化
      let that = this;
      return new Promise(function(resolve, reject){
        if (arg === 1) {  
          that.ipagination.current = 1;
        }
        that.loading = true;
        let params = that.getQueryParams(); // 查询条件
        CRUD.getList(params)
          .then((res) => {
            resolve(res);
            // 页码
            that.ipagination.total = res.result.total;
          })
          .catch((res) => {
            reject(res);
          })
          .finally(() => {
            that.loading = false;
          });
      })
    },
    // 增-弹窗
    crudCreate(data) {
      this.$refs.updateForm.crudCreate(data);
    },
    // 改-弹窗
    crudUpdate(data) {
      this.$refs.updateForm.crudUpdate(data);
    },
    // 删
    crudDelete(id) {
      const that = this;
      let param = [];
      param.push(id);
      CRUD.del(param)
        .then((res) => {
          that.$message.success('删除成功');
        })
        .finally(() => {
          that.loadData();
        });
    },
    // 查
    searchQuery() {
      this.loadData(1);
    },
    // 查 重置
    searchReset() {
      this.queryParam = {};
      this.loadData(1);
    },

    // 弹窗-关闭
    closeModal() {
      this.loadData();
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      this.ipagination = pagination;
      this.loadData();
    },
    // 查询条件
    getQueryParams(){
      var param = this.queryParam;
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return param
    },
    // 多选
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
  },
  filters: {
    //金钱分转元
    money: function (money) {
      if (money == 0) {
        return '0.00'
      } else if (money > 0) {
        return (money / 100).toFixed(2)
      } else {
        return ''
      }
    },
  }
};
let CRUD = {}
export function crud(options) {
  CRUD = options
  return options;
}
